﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;


public partial class Controls_Galleries_ctrGalleryList : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    private int nItemID = 0;
    private int nPageSize = 6;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        if (!IsPostBack)
        {
            this.setTitle();
            BigGallary();
        }
    }
    private void BigGallary()
    {
        int nPageCount = 0;
        int nAlbumID = ConvertData.ConvertToInt(Request.QueryString[Config.ALBUM_ID]);
        if(nAlbumID==0)
        {
            Albums objAlbum = new Albums();
            DataTable dtbAlbum = new DataTable();
            dtbAlbum = objAlbum.Search(0, "", 1, 1000, ref nPageCount);
            int nCount = dtbAlbum.Rows.Count;
            if (nCount > 0)
            {
                nAlbumID = ConvertData.ConvertToInt(dtbAlbum.Rows[nCount - 1]["AlbumID"]);
            }
        }

        AlbumImages objAlbumDetails = new AlbumImages();
        DataTable dtbAlbumDetails = new DataTable();
        int nPagecountDetails = 0;
        dtbAlbumDetails = objAlbumDetails.Search(nAlbumID, 1, 1000, ref nPagecountDetails);
        rptSlider.DataSource = dtbAlbumDetails;
        rptSlider.DataBind();

        BindData(1, nAlbumID);
        
    }
    protected void setTitle()
    {
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        lblCategoryName.Text = MultipleLanguage.GALLERY[nCurrentLang - 1];
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }
    private void BindData(int CurrentPageIndex, int nItem)
    {
        try
        {
            int nPageCount = 0;

            Albums objAlbumImage = new Albums();
            DataTable dtbAlbumImage = new DataTable();
            dtbAlbumImage = objAlbumImage.Search(nItem, "", 1, 1000,ref nPageCount);
            rptGallery.DataSource = dtbAlbumImage;
            rptGallery.DataBind();

            VisiblePaging(dtbAlbumImage.Rows.Count);
            if (dtbAlbumImage.Rows.Count > 0)
                tblWarning.Visible = false;
            else
                tblWarning.Visible = true;
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    private void initDataPaging()
    {
        Pager.PageSize = nPageSize;
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.BindData(currnetPageIndx, nItemID);
    }
    private void VisiblePaging(int intPagecount)
    {
        if (intPagecount > nPageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }

    protected void rptGallery_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = "~/Gallery.aspx" + "?" + Config.ALBUM_ID + "=" + nID;
                Response.Redirect(sDetail);
                break;
        }
    }
    protected void rptGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ImageButton imgImage = (ImageButton)e.Item.FindControl("imgImage");
        HyperLink lnkGallery = (HyperLink)e.Item.FindControl("lnkGallery");

        switch (nCurrentLang)
        {
            case 1:
                lnkGallery.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumName1"));
                break;
            case 2:
                lnkGallery.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumName2"));
                break;
            case 3:
                lnkGallery.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumName3"));
                break;
        }
        lnkGallery.NavigateUrl = "~/Gallery.aspx" + "?" + Config.ALBUM_ID + "=" + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumID")); ;
        imgImage.ImageUrl = Config.ROOT + Config.IMAGE_GALLERY_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumImage"));
        imgImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumID"));
    }
    protected void rptSlider_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Image imgImage = (Image)e.Item.FindControl("imgImage");
        imgImage.ImageUrl = Config.ROOT + Config.IMAGE_GALLERY_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "AlbumID")) + "/" + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageName"));
    }
}